home *** CD-ROM | disk | FTP | other *** search
- / This is a simple fixed array with arbitrary integer lower and upper bounds
- / and index range checking.
- / The array class uses assertions to check various error conditions,
- / including index out-of-range and failed memory allocation.
- / Failed assertions cause program termination, and posssibly a core dump.
- /
- / The element type must have a default constructor
- / (a constructor that takes no arguments) or be of a type that does
- / not need a constructor, such as, integers and other built-in types.
- /
- / The copy-constructor of the array should use the copy-constructor of the
- / element type to initialize its elements. Unfortunately, this doesn't
- / work (due to a compiler bug?), so there is no copy-constructor for arrays.
- /
- / It must also be possible to assign to the element type, and to
- / destroy elements.
- /
- / Author: Dag Bruck, Department of Automatic Control, Lund Institute of
- / Technology, Box 188, S-221 00 Lund, Sweden. E-mail: dag@control.lth.se
- /
- / $Id: array.H,v 1.4 1992/07/07 12:38:28 dag Exp $
-
- ifndef ARRAY_H
- define ARRAY_H
-
- emplate <class T>
- lass Array {
- ublic:
- Array(int lower_bound, int upper_bound);
- // Creates an array with given lower and upper bounds. The upper
- // bound must be greater than the lower bound. The elements of the
- // array are initialized by the default constructor of the element type.
-
- ~Array();
- // Destroys the array and its elements.
-
- T& operator [] (int);
- const T& operator [] (int) const;
- // Returns (a reference to) an element of the array. The index must
- // be in the array's bounds.
-
- Array<T>& operator = (const T &);
- // Assigns a new value to all elements of the array.
-
- Array<T>& operator = (const Array<T> &);
- // Assigns one array to another. The bounds of the arrays
- // must be the same.
-
- void bounds(int& lower_bound, int& upper_bound) const;
- // Returns the lower and upper bounds of the array, as given
- // to the constructor.
-
- unsigned size() const { return sz; }
- // Returns the number of elements in the array.
-
- T* storage() { return data; }
- // Returns a pointer to the internal data space of the array.
- // The internal data space is never re-allocated during the lifetime
- // of the array.
- //
- // Using this operation is potentially dangerous because it defies
- // index range checking, and the pointer may be left dangling
- // after the array has been destroyed.
-
- rivate:
- T* data;
- const int low;
- const unsigned sz;
-
- Array(const Array<T> &);
- // No copy-constructor.
- ;
-
- endif
-